﻿using Renci.SshNet;
using System;
using System.IO;
using System.Text;

namespace IceDog.SSHConnection.SSHDotNet.Cmd
{
    class Program
    {
        static void Main(string[] args)
        {
            var username = "root";
            var connectionInfo = new ConnectionInfo(
                host: "192.168.0.124",
                port: 22,
                username: username,
                new KeyboardInteractiveAuthenticationMethod(username),
                new PasswordAuthenticationMethod(username, password: "edoc2@love00")
                );

            //Scp下载文件
            using (var client = new ScpClient(connectionInfo))
            {
                client.Connect();
                client.Downloading += Client_Downloading;
                client.Download("/1.txt", new FileInfo(@"D:\1.txt"));
            };

            //Scp上传文件
            using (var client = new ScpClient(connectionInfo))
            {
                client.Connect();
                client.Uploading += Client_Uploading;
                client.Upload(new FileInfo(@"D:\edit-hosts.bat"), "/home/bat-script/edit.bat");

            };


            //SSH执行命令
            using (var client = new SshClient(connectionInfo))
            {
                client.Connect();
                SshCommand command = client.CreateCommand("echo hello > /1.txt", Encoding.UTF8);
                var resutl = command.Execute();
                Console.WriteLine(resutl);
            }
        }

        private static void Client_Downloading(object sender, Renci.SshNet.Common.ScpDownloadEventArgs e)
        {
            Console.WriteLine(e.Filename);
        }

        private static void Client_Uploading(object sender, Renci.SshNet.Common.ScpUploadEventArgs e)
        {
            Console.WriteLine(e.Filename);
        }
    }
}
